#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTime>
#include <windows.h>

void Widget::timerEvent(QTimerEvent *e)
{
  if(NULL!=tcpSocket1&&NULL!=tcpSocket2&&state==false)
  {
    state=true;
    if (rand()%100>=50)
    {
      T1();
    }
    else {
      T2();
    }
  }
}

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
  ui->setupUi(this);
  setWindowTitle(tr("服务器"));
  timeID=startTimer(2000);
  srand(time(NULL));
  tcpSocket1 =NULL;
  tcpSocket2 =NULL;
  tcpSocket3 =NULL;
  tcpServer=new QTcpServer(this);
  tcpSocket1=new QTcpSocket(this);
  tcpSocket2=new QTcpSocket(this);
  tcpSocket3=new QTcpSocket(this);
  ip1="0";
  ip2="0";
  port1=-1;
  port2=-1;

  //QHostAddress a(tr("cn-cd-dx.sakurafrp.com"));
  QHostAddress a(tr("127.0.0.1"));
  tcpServer ->listen(a, 23333);


  connect(tcpServer, &QTcpServer::newConnection,
          [=]()
          {
            connect(this,&Widget::T1,[=]{
              tcpSocket1->write("开始");
              ui->textEdit->append(QString("开始1"));
            });

            connect(this,&Widget::T1,[=]{
              tcpSocket2->write("开始");
              ui->textEdit->append(QString("开始2"));
            });

            if(ip1=="0"&&port1==-1)
            {
              tcpSocket1 = tcpServer->nextPendingConnection();
              ip1 = tcpSocket1->peerAddress().toString();
              port1 = tcpSocket1->peerPort();
              QString temp = QString("玩家1[%1,%2]:成功连接").arg(ip1).arg(port1);
              ui->textEdit->append(temp);
            }
            else if (ip2=="0"&&port2==-1) {
              tcpSocket2 = tcpServer->nextPendingConnection();
              ip2 = tcpSocket2->peerAddress().toString();
              port2 = tcpSocket2->peerPort();
              QString temp = QString("玩家2[%1,%2]:成功连接").arg(ip2).arg(port2);
              ui->textEdit->append(temp);


            }
            else{
              tcpSocket3 = tcpServer->nextPendingConnection();

              tcpSocket3->write("服务器人数已满");
              tcpSocket3->disconnectFromHost();
              tcpSocket3->close();
              tcpSocket3=NULL;
            }

            connect(tcpSocket1,&QTcpSocket::readyRead,[=]{
              QString s=tcpSocket1->readAll().toStdString().data();
              ui->textEdit->append(s);
              b++;
              ui->textEdit->append(QString("%1").arg(b));
              /*if(b%4==0)
              {
                tcpSocket2->write("j");
                tcpSocket1->write("j");
                Sleep(50);
              }*/
              if(NULL!=tcpSocket2)
              {
                tcpSocket2->write(s.toUtf8().data());
                if("认输"==s)
                {
                  state=false;
                }
              }
            });
            connect(tcpSocket2,&QTcpSocket::readyRead,[=]{
              QString s=tcpSocket2->readAll().toStdString().data();
              ui->textEdit->append(s);
              b++;
              ui->textEdit->append(QString("%1").arg(b));
              /*if(b%4==0)
              {
                tcpSocket2->write("j");
                tcpSocket1->write("j");
                Sleep(50);
              }*/
              if(NULL!=tcpSocket1)
              {
                tcpSocket1->write(s.toUtf8().data());
                if("认输"==s)
                {
                  state=false;
                }
              }
            });



            connect(tcpSocket1,&QTcpSocket::disconnected,[=]{
              ui->textEdit->append(QString("玩家1断开连接"));
              ip1="0";
              port1=-1;
              tcpSocket1=NULL;
            });
            connect(tcpSocket2,&QTcpSocket::disconnected,[=]{
              ui->textEdit->append(QString("玩家2断开连接"));
              ip2="0";
              port2=-1;
              tcpSocket2=NULL;
            });





          });
  connect(ui->close1,&QPushButton::clicked,[=]{
    if(tcpSocket1!=NULL){
      tcpSocket1->write("开始");
      ui->textEdit->append(QString("开始1"));
    }
  });
  connect(ui->close2,&QPushButton::clicked,[=]{
    if(tcpSocket1!=NULL){
      tcpSocket2->write("开始");
      ui->textEdit->append(QString("开始2"));
    }
  });



}

Widget::~Widget()
{
  delete ui;
}


